/**
 * @author 老纳慢走
 * 
 */
var count = 10;
var content = "";

$(document).ready(function(){
	getUsers();
});

function getUsers(){
	$.get($("#users_url").val(),function(data){
		var users = JSON.parse(data);
		var userHtml = "";
		if(users.length > 0){
			users.forEach(function(value, index){
				userHtml += '<div class="b-b" style="line-height:30px;padding-left: 14px;">';
				userHtml += '<input type="radio" name="selectedUser" style="-webkit-appearance:radio;" onclick=\'choose_user(';
				userHtml += '"' + value.truename + '","'+ value.tel + '","' + value.address+'")\'';
				if(value.is_default == 1){
					userHtml += " checked >";
					choose_user(value.truename, value.tel, value.address);
				}else{
					userHtml += " >"
				}
				userHtml += '<label>' + value.truename + " " + value.tel + " </label";
				userHtml += '<label>' + value.address + '</label></div>';

			});
		}
		$("#users").empty();
		$("#users").append(userHtml);
	});
}

function getTicket(no){
	ajax_post_json("apis/message/ticket", {"order_no":no, "access_token":access_token}, m_beforeSend,  ticket_success, detail_error);
}

function m_beforeSend(){
	$("#ticket_btn").attr("disabled","true");
	$("#s_ticket").html("正在请求中...");
};

function ticket_success(data) {
	if (data.result==true){
		$("#s_ticket").html(data.content);
		
		content = 	$("#ticket_btn").val() ;
		$("#ticket_btn").val(content+ " (" +count+ ")");
		setTimeout("countdown()", 1000);
	}
};

function countdown(){
	count--;
	if (count > 0) {
		$("#ticket_btn").val(content+ " (" +count+ ")");
		setTimeout("countdown()", 1000);
	}else{
		$("#ticket_btn").removeAttr("disabled");
		$("#ticket_btn").val(content);
		$("#s_ticket").html("");
		count = 10;
	}
}

function detail_error(XMLHttpRequest, textStatus, errorThrown){
	$("#ticket_btn").removeAttr("disabled");
	$("#s_ticket").html("网络错误");
	count = 10;
	
}

function flexible_animate() {
	$("#order_detail").toggleClass("b-b");
	
	$("#order_detail_items").animate({
		height : 'toggle',
		opacity : 'toggle',
		duration : "slow"
	}, function(){
		$("#order_detail").toggleClass("mi_down_arrow");
	});
}

function choose_user(turename, tel, address){
	$("#name").val(turename);
	$("#tel").val(tel);
	$("#address").val(address);
}